package com.example.NearByLocation;

import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2014/11/28.
 */
public class HttpClientManager {

    public List<JsonData> getData(String keyWord,int range,double longi,double leni,int page) throws IOException, JSONException {
        List<JsonData> list = new ArrayList<JsonData>();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("source","3000947560"));
        params.add(new BasicNameValuePair("access_token","2.00NmGLLEAYfFRD8e4970c2f34sEyUC"));
        params.add(new BasicNameValuePair("count",10+""));
        params.add(new BasicNameValuePair("page",page+""));
//        params.add(new BasicNameValuePair("output","json"));
        params.add(new BasicNameValuePair("q",keyWord));
//        params.add(new BasicNameValuePair("city",city));
        params.add(new BasicNameValuePair("range",range+""));
        params.add(new BasicNameValuePair("coordinate",longi+","+leni));
        String listStr = URLEncodedUtils.format(params, "utf-8");
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("https://api.weibo.com/2/location/pois/search/by_geo.json?" + listStr);
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();
        InputStream inputStream = httpEntity.getContent();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, len);
        }
        String receive = byteArrayOutputStream.toString();
        JSONObject jsonObject = new JSONObject(receive);
        JSONArray jsonArray = jsonObject.optJSONArray("poilist");
        if (jsonArray == null) {
            return list;
        }else {
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                String name = object.optString("name");
                String add = object.optString("address");
                String distance = object.optString("distance");
                String tel = object.optString("tel");
                String dataLongi = object.optString("x");
                String dataLeni = object.optString("y");
                JsonData poi = new JsonData();
                poi.setName(name);
                poi.setAdd(add);
                poi.setDistance(distance);
                poi.setPhoneNo(tel);
                poi.setLongi(dataLongi);
                poi.setLeni(dataLeni);
                list.add(poi);
            }
        }
        Log.d("json1",list.size()+"");
        return list;
    }
}
